﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmRamaDiscipulado : Form
    {
        [Dependency]
        public IPersonaBL personaBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        [Dependency]
        public IDiscipuladoBL discipuloBL { get; set; }
        public FrmRamaDiscipulado()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public USUARIO currentUsuarioRamaDiscipulado { get; set; }

        private void FrmRamaDiscipulado_Load(object sender, EventArgs e)
        {
            if (currentUsuarioRamaDiscipulado != null)
            {
                menuLlena();
            }
            else
                CargarDatos();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioRamaDiscipulado.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.BtnEditarDiscipulo,this.BtnEditarPersona};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarDatos();
        }

        public void CargarDatos()
        {
            pERSONABindingSource.DataSource = personaBL.GetAllFromPersona();
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            pERSONABindingSource.DataSource = personaBL.GetFromPersonaByDescripcion(TxtDescripcion.Text);
        }

        private void pERSONADataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.pERSONADataGridView.Columns[e.ColumnIndex].Name.Equals("VerDetalle"))
            {
                var currentPersona = pERSONABindingSource.Current as PERSONA;
                if (currentPersona != null)
                {
                    FrmMttoPersona frm = new FrmMttoPersona();
                    frm.CurrentPersona = currentPersona;
                    frm.VerDetalle();
                    frm.ShowDialog();
                }
                else
                {
                    MessageBox.Show(@"Debe seleccionar una Persona", @"Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }       
            }
        }

        private void pERSONADataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.pERSONADataGridView1.Columns[e.ColumnIndex].Name.Equals("VerDetalleDiscipulo"))
            {
                var currentPersona = dISCIPULOSBindingSource.Current as PERSONA;
                if (currentPersona != null)
                {
                    FrmMttoPersona frm = new FrmMttoPersona();
                    frm.CurrentPersona = currentPersona;
                    frm.VerDetalle();
                    frm.ShowDialog();
                }
                else
                {
                    MessageBox.Show(@"Debe seleccionar una Persona", @"Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void BtnEditarPersona_Click(object sender, EventArgs e)
        {
            var currentPersona = pERSONABindingSource.Current as PERSONA;
            if (currentPersona.Id != 1)
            {
                if (currentPersona != null)
                {
                    FrmMttoPersona frm = new FrmMttoPersona();
                    frm.CurrentPersona = currentPersona;
                    frm.Editar();
                    frm.ShowDialog();
                    Refrescar();
                }
                else
                {
                    MessageBox.Show(@"Debe seleccionar una Persona", @"Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void Refrescar()
        {
            pERSONABindingSource.DataSource = discipuloBL.GetAllFromDiscipulado();
        }

        private void BtnEditarDiscipulo_Click(object sender, EventArgs e)
        {
            var currentPersona = dISCIPULOSBindingSource.Current as PERSONA;
            if (currentPersona.Id != 1)
            {
                if (currentPersona != null)
                {
                    FrmMttoPersona frm = new FrmMttoPersona();
                    frm.CurrentPersona = currentPersona;
                    frm.Editar();
                    frm.ShowDialog();
                    Refrescar();
                }
                else
                {
                    MessageBox.Show(@"Debe seleccionar una Persona", @"Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
